﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Common
{
    public class CommonHelper
    {
        public static string CalcMD5(string str)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
            return CalcMD5(bytes);
        }

        public static string CalcMD5(byte[] bytes)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] computeBytes = md5.ComputeHash(bytes);
                string result = "";
                for (int i = 0; i < computeBytes.Length; i++)
                {
                    result += computeBytes[i].ToString("X").Length == 1 ? "0" + computeBytes[i].ToString("X") : computeBytes[i].ToString("X");
                }
                return result;
            }
        }

        public static string CalcMD5(Stream stream)
        {
            using (MD5 md5 = MD5.Create())
            {
                stream.Position = 0;
                byte[] computeBytes = md5.ComputeHash(stream);
                string result = "";
                for (int i = 0; i < computeBytes.Length; i++)
                {
                    result += computeBytes[i].ToString("X").Length == 1 ? "0" + computeBytes[i].ToString("X") : computeBytes[i].ToString("X");
                }
                return result;
            }
        }
        
        private static char GetOper()
        {
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            char[] str = { '+', '-', '*', '$' };
            return str[rd.Next(0, 4)];
        }

        public static string GetPhoneValidCode()
        {
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            return rd.Next(100000, 999999).ToString();
        }

        //Chapcha
        public static string CreateVerifyCode(out string res)
        {
            int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            StringBuilder sb = new StringBuilder();
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            int a = data[rd.Next(0, 10)];
            int b = data[rd.Next(0, 10)];
            switch (GetOper())
            {
                case '+':
                    res = (a + b).ToString();
                    sb.Append(a)
                        .Append("加")
                        .Append(b)
                        .Append("等于几");
                    return sb.ToString();
                case '-':
                    if (a < b)
                    {
                        a = a + b;
                        b = a - b;
                        a = a - b;
                    }
                    res = (a - b).ToString();
                    sb.Append(a)
                        .Append("减")
                        .Append(b)
                        .Append("等于几");
                    return sb.ToString();
                case '*':
                    res = (a * b).ToString();
                    sb.Append(a)
                        .Append("乘")
                        .Append(b)
                        .Append("等于几");
                    return sb.ToString();
                case '$':
                default:
                    for (int i = 0; i < 4; i++)
                    {
                        sb.Append(data[rd.Next(0, 10)]);
                    }
                    res = sb.ToString();
                    return res;
            }
        }

        public static string GetSalt()
        {
            StringBuilder sb = new StringBuilder();
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            int len = rd.Next(6, 10);
            char[] ch = { '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', '?', ':', '<', '>' };
            for (int i = 0; i < len; i++)
            {
                sb.Append(ch[rd.Next(ch.Length)]);
            }
            return sb.ToString();
        }

    }
}
